package com.ruoyi.web.controller.teacher;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.BasisCaseLoading;
import com.ruoyi.domain.BasisCaseLoadingTask;
import com.ruoyi.domain.BasisSnapshot;
import com.ruoyi.mapper.BasisCaseLoadingMapper;
import com.ruoyi.mapper.BasisCaseLoadingTaskMapper;
import com.ruoyi.mapper.BasisSnapshotMapper;
import com.ruoyi.service.IBasisCaseLoadingService;
import com.ruoyi.service.IBasisCaseLoadingTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

//快照接口
@RestController
@RequestMapping("/basisSnapshot")
public class BasisSnapshotController {

    @Autowired
    BasisSnapshotMapper basisSnapshotMapper;

    @Autowired
    BasisCaseLoadingMapper basisCaseLoadingMapper;

    @Autowired
    BasisCaseLoadingTaskMapper basisCaseLoadingTaskMapper;
    @Value("${snapshot.path}")
    private String snapshotPath;


    private final long l = System.currentTimeMillis();

    @PostMapping("/uploadFile")
    public String uploadFile(@RequestParam("file") MultipartFile file,String loadingId,String taskId,String currentTime,String timeRange,String times) {

//        String uploadPath = "D:\\work\\testFile"; // 上传目标路径

//        BasisCaseLoading basisCaseLoading = basisCaseLoadingMapper.selectBasisCaseLoadingById(loadingId);
        try {
            // 2. 创建目标文件夹（如果不存在）
            File uploadDir = new File(snapshotPath+"\\"+loadingId+"\\"+l);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();

            }

            // 3. 生成带时间戳的新文件名（保留原扩展名）
            String originalFilename = file.getOriginalFilename();
            // 提取文件扩展名（例如：image.jpg -> .jpg）
            String fileExtension = originalFilename.lastIndexOf(".") > 0
                    ? originalFilename.substring(originalFilename.lastIndexOf("."))
                    : "";
            // 生成新文件名（时间戳+扩展名）
            String newFilename = System.currentTimeMillis() + fileExtension;

            // 4. 构造目标文件对象
            File targetFile = new File(uploadDir, newFilename);

            // 5. 执行文件保存
            file.transferTo(targetFile);

            SysUser user = SecurityUtils.getLoginUser().getUser();

            BasisCaseLoadingTask basisCaseLoadingTask = basisCaseLoadingTaskMapper.selectBasisCaseLoadingTaskByTaskId(taskId);
            BasisSnapshot basisSnapshot = new BasisSnapshot();
            basisSnapshot.setLoadingId(loadingId);
            basisSnapshot.setTaskId(taskId);
            basisSnapshot.setUserId(basisCaseLoadingTask.getUid());
            basisSnapshot.setCreateTime(new Date());
            basisSnapshot.setCreateUid(user.getUserId()+"");
            basisSnapshot.setCurrentTime(currentTime);
            basisSnapshot.setTimes(times);
            basisSnapshot.setTimeRange(timeRange);

            basisSnapshot.setFilePath(snapshotPath+"/"+loadingId+"/"+l+"/"+newFilename);
            basisSnapshot.setPath(loadingId+"/"+l+"/"+newFilename);
            basisSnapshotMapper.addBasisSnapshot(basisSnapshot);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";

    }


    @GetMapping("/findByTime")
    public AjaxResult findByTime (String currentTime){
      return AjaxResult.success(basisSnapshotMapper.findByTime(currentTime));
    }

    @GetMapping("/findByloadingIdFormBasisSnapshot")
    public AjaxResult findByloadingIdFormBasisSnapshot (String loadingId){
        return AjaxResult.success(basisSnapshotMapper.findByloadingIdFormBasisSnapshot(loadingId));
    }

}
